﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports System.Windows.Data

Namespace Converters

    Public Class VisibilityConverter
        Implements IValueConverter
        Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            If value Is Nothing Then
                Return Visibility.Collapsed
            End If

            Dim visibile As Boolean = False
            Dim reverse As Boolean = False

            Boolean.TryParse(value.ToString(), visibile)
            If parameter IsNot Nothing Then
                Boolean.TryParse(parameter.ToString(), reverse)
            End If

            Return If((Not reverse), If(visibile, Visibility.Visible, Visibility.Collapsed), If(visibile, Visibility.Collapsed, Visibility.Visible))
        End Function

        Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Dim visibilityObject As Visibility

            Try
                visibilityObject = CType(value, Visibility)
            Catch
                visibilityObject = Visibility.Collapsed
            End Try

            Return If(visibilityObject = Visibility.Visible, True, False)
        End Function
    End Class
End Namespace